<?php
//系统属性
class SysattrAction extends Action{
   public function index(){
	  $parid=$_GET["parid"]?$_GET["parid"]:0;
	  if(!is_numeric($parid))alert('参数错误');
	  $this->assign("parid",$parid);
	  
	  $col_id=$_GET["col_id"];
	  if(empty($col_id)||!is_numeric($col_id))alert('参数错误');
	  $this->assign("col_id",$col_id);
	  
	  $sysattr=M("sysattr");
	  $map["col_id"]=$col_id;
	  $map["parid"]=$parid;
	  $attr_list=$sysattr->where($map)->order("sequence desc")->findAll();
	  //如果为空,转到添加
	  if(empty($attr_list))$this->redirect("Sysattr/add",array("col_id"=>$col_id,"parid"=>$parid),0,'');
	  
	  if(!empty($parid)){
		 $maxseq=$sysattr->where("parid=$parid")->max("sequence");
		 $minseq=$sysattr->where("parid=$parid")->min("sequence");
		 $this->assign("maxseq",$maxseq);
		 $this->assign("minseq",$minseq);
		 
		 foreach($attr_list as $k=>$v){
			 $parid=$v['parid'];
			 $attr_list[$k]['par_name']=$sysattr->where("id=$parid")->getField("attr_name"); 
		 }
	  }
	  $this->assign("attr_list",$attr_list);
	  
	  $this->display();   
   }
   public function add(){
		$parid=$_GET["parid"]?$_GET["parid"]:0;
		if(!is_numeric($parid))alert('参数错误');
		$this->assign("parid",$parid);
		
		$col_id=$_GET["col_id"];
		if(empty($col_id)||!is_numeric($col_id))alert('参数错误');
		$this->assign("col_id",$col_id);
	   
	   $this->display();
	   
   }
   public function insert(){
	    checkpost();
		
	    $parid=$_GET["parid"]?$_GET["parid"]:0;
		if(!is_numeric($parid))alert('参数错误');
		
		$col_id=$_GET["col_id"];
		if(empty($col_id)||!is_numeric($col_id))alert('参数错误');
		
		$_POST["col_id"]=$col_id;
		$_POST["parid"]=$parid;

		$sysattr=M("sysattr");
		
		if(!empty($parid)){
		  $maxseq=$sysattr->where("parid=$parid")->max("sequence");
		  $_POST["sequence"]=++$maxseq;	
		}
		
		$sysattr->add($_POST);
		
		$this->redirect("Sysattr/index",array("col_id"=>$col_id,"parid"=>$parid),1,'操作成功');
	   
   }
   public function edit(){
	  
	  $id=$_GET["id"];
	  
	  if(empty($id)||!is_numeric($id))alert('参数错误'); 
	   
	   
	  $parid=$_GET["parid"]?$_GET["parid"]:0;
	  if(!is_numeric($parid))alert('参数错误');
	  $this->assign("parid",$parid);
	  
	  $col_id=$_GET["col_id"];
	  if(empty($col_id)||!is_numeric($col_id))alert('参数错误');
	  $this->assign("col_id",$col_id);
	  
	  $sysattr=M("sysattr");
	  
	  $sysattr_info=$sysattr->where("id=$id")->find();
	  
	  $this->assign("sysattr_info",$sysattr_info);
	  
	  $this->display();
	   
   }
   public function update(){
	  
	  checkpost(); 
	   
	  $id=$_POST["id"];
	  
	  if(empty($id)||!is_numeric($id))alert('参数错误'); 
	   
	   
	  $parid=$_GET["parid"]?$_GET["parid"]:0;
	  if(!is_numeric($parid))alert('参数错误');
	  
	  $col_id=$_GET["col_id"];
	  if(empty($col_id)||!is_numeric($col_id))alert('参数错误');
	  
	  $sysattr=M("sysattr");
	  
	  $sysattr->save($_POST);
	  
	  $this->redirect("Sysattr/index",array("col_id"=>$col_id,"parid"=>$parid),1,'操作成功'); 
	   
   }
   public function delete(){
	   checkget();
	   
	   $id=$_GET["id"];
	   if(empty($id)||!is_numeric($id))alert('参数错误');
	   
	   $parid=$_GET["parid"]?$_GET["parid"]:0;
	   if(!is_numeric($parid))alert('参数错误');
		
	   $col_id=$_GET["col_id"];
	   if(empty($col_id)||!is_numeric($col_id))alert('参数错误');
	   
	   $sysattr=M("sysattr");
	   $sysattr->delete($id);
	   
	   if(empty($parid))$sysattr->where("parid=$id")->delete();
	   
	   $this->redirect("Sysattr/index",array("col_id"=>$col_id,"parid"=>$parid),1,'操作成功');  
   }
   public function up(){
	   checkget();
	   
	   $id=$_GET["id"];
	   if(empty($id)||!is_numeric($id))alert('参数错误');
	   
	   $col_id=$_GET["col_id"];
	   if(empty($col_id)||!is_numeric($col_id))alert('参数错误');
	   
	   $parid=$_GET["parid"]?$_GET["parid"]:0;
	   if(!is_numeric($parid))alert('参数错误');
	   
	   $sysattr=M("sysattr");
	   $c=$sysattr->where("id=$id")->find();
	   
	   $seq=$c["sequence"];
	   $parid=$c["parid"];
	       
	   $n=$sysattr->where("parid=$parid and sequence > $seq and col_id=$col_id")->order("sequence asc")->find();
	   
	   $nextseq=$n["sequence"];
	   $nextid=$n["id"];

       //修改
	   $sysattr->where("id=$nextid")->setField("sequence",$seq);
	   $sysattr->where("id=$id")->setField("sequence",$nextseq);
	   
	   $this->redirect("Sysattr/index",array("col_id"=>$col_id,"parid"=>$parid));  
	   
   }
   public function down(){
	   checkget();
	   
	   $id=$_GET["id"];
	   if(empty($id)||!is_numeric($id))alert('参数错误');
	   
	   $col_id=$_GET["col_id"];
	   if(empty($col_id)||!is_numeric($col_id))alert('参数错误');
	   
	   $parid=$_GET["parid"]?$_GET["parid"]:0;
	   if(!is_numeric($parid))alert('参数错误');
	   
	   $sysattr=M("sysattr");
	   
	   $c=$sysattr->where("id=$id")->find();
	
	   $seq=$c["sequence"];
	   $parid=$c["parid"];
	   
	   $n=$sysattr->where("parid=$parid and sequence < $seq and col_id=$col_id")->order("sequence desc")->find();
	   
	   $nextseq=$n["sequence"];
	   $nextid=$n["id"];
       //修改
	   $sysattr->where("id=$nextid")->setField("sequence",$seq);
	   $sysattr->where("id=$id")->setField("sequence",$nextseq);
       
	  $this->redirect("Sysattr/index",array("col_id"=>$col_id,"parid"=>$parid));
	   
   }
	
}
?>